package jagex;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class CacheLoader {

	public CacheLoader(InputStream arg0) {
		closingStream = false;
		inputStream = arg0;
	}

	public CacheLoader(Socket arg0) throws IOException {
		closingStream = false;
		streamSocket = arg0;
		inputStream = arg0.getInputStream();
		outputStream = arg0.getOutputStream();
	}

	public CacheLoader(String arg0) throws IOException {
		closingStream = false;
		inputStream = DataOperations.streamFromPath(arg0);
	}

	public CacheLoader(byte arg0[]) {
		closingStream = false;
		outputBuffer = arg0;
		yd = 0;
		closingStream = true;
	}

	public CacheLoader(byte arg0[], int arg1) {
		closingStream = false;
		outputBuffer = arg0;
		yd = arg1;
		closingStream = true;
	}

	public void closeStream() {
		if (closingStream) {
			return;
		}
		if (outputStream != null) {
			try {
				outputStream.flush();
			} catch (IOException _ex) {

			}
		}
		try {
			if (streamSocket != null) {
				streamSocket.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
			if (outputStream != null) {
				outputStream.close();
				return;
			}
		} catch (IOException _ex) {
			System.out.println("Error closing stream");
		}
	}

	public int readInputStream() throws IOException {
		if (outputBuffer != null) {
			return outputBuffer[yd++];
		}
		if (closingStream) {
			return 0;
		} else {
			return inputStream.read();
		}
	}

	public int readByte() throws IOException {
		return readInputStream();
	}

	public int read2ByteInt() throws IOException {
		int i = readByte();
		int j = readByte();
		return i * 256 + j;
	}

	public int inputStreamAvailable() throws IOException {
		if (closingStream) {
			return 0;
		} else {
			return inputStream.available();
		}
	}

	public void readInputStream(int arg0, byte arg1[]) throws IOException {
		if (closingStream) {
			return;
		}
		int i = 0;
		boolean flag = false;
		int j;
		for (; i < arg0; i += j) {
			if ((j = inputStream.read(arg1, i, arg0 - i)) <= 0) {
				throw new IOException("EOF");
			}
		}
	}

	public void qb() throws IOException {
		for (int i = readInputStream(); i != 61 && i != -1; i = readInputStream()) {
			;
		}
	}

	public int vb() throws IOException {
		int i = 0;
		boolean flag = false;
		int j;
		for (j = readInputStream(); j < 48 || j > 57;) {
			if (j == 45) {
				flag = true;
			}
			j = readInputStream();
			if (j == -1) {
				throw new IOException("Eof!");
			}
		}

		for (; j >= 48 && j <= 57; j = readInputStream()) {
			i = (i * 10 + j) - 48;
		}

		if (flag) {
			i = -i;
		}
		return i;
	}

	public String wb() throws IOException {
		String s = "";
		boolean flag = false;
		int i;
		for (i = readInputStream(); i < 32 || i == 44 || i == 59 || i == 61;) {
			i = readInputStream();
			if (i == -1) {
				throw new IOException("Eof!");
			}
		}

		if (i == 34) {
			flag = true;
			i = readInputStream();
		}
		for (; i != -1; i = readInputStream()) {
			if (!flag && (i == 44 || i == 61 || i == 59) || flag && i == 34) {
				break;
			}
			s = s + le[i];
		}

		return s;
	}

	public int rb() throws IOException {
		int i = 0;
		int j;
		for (j = readInputStream(); (j < 48 || j > 57) && (j < 97 || j > 102)
				&& (j < 65 || j > 70);) {
			j = readInputStream();
			if (j == -1) {
				throw new IOException("Eof!");
			}
		}

		do {
			if (j >= 48 && j <= 57) {
				i = (i * 16 + j) - 48;
			} else if (j >= 97 && j <= 102) {
				i = (i * 16 + j + 10) - 97;
			} else {
				if (j < 65 || j > 70) {
					break;
				}
				i = (i * 16 + j + 10) - 65;
			}
			j = readInputStream();
		} while (true);
		return i;
	}

	protected InputStream inputStream;
	protected OutputStream outputStream;
	protected Socket streamSocket;
	protected boolean closingStream;
	protected byte outputBuffer[];
	int yd;
	final int zd = 61;
	final int ae = 59;
	final int be = 42;
	final int ce = 43;
	final int de = 44;
	final int ee = 45;
	final int fe = 46;
	final int ge = 47;
	final int he = 92;
	final int ie = 32;
	final int je = 124;
	final int ke = 34;
	static char le[];

	static {
		le = new char[256];
		for (int i = 0; i < 256; i++) {
			le[i] = (char) i;
		}

		le[61] = '=';
		le[59] = ';';
		le[42] = '*';
		le[43] = '+';
		le[44] = ',';
		le[45] = '-';
		le[46] = '.';
		le[47] = '/';
		le[92] = '\\';
		le[124] = '|';
		le[33] = '!';
		le[34] = '"';
	}
}
